package com.liangzi.service_core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liangzi.liangzi_common.exception.Assert;
import com.liangzi.liangzi_common.result.R;
import com.liangzi.liangzi_common.result.ResponseEnum;
import com.liangzi.service_core.mapper.*;
import com.liangzi.service_core.pojo.entity.*;
import com.liangzi.service_core.pojo.vo.VisitBookingQueryVO;
import com.liangzi.service_core.service.VisitBookingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 患者预约表 服务实现类
 * </p>
 *
 * @author zhj
 * @since 2021-12-13
 */
@Service
@Slf4j
public class VisitBookingServiceImpl extends ServiceImpl<VisitBookingMapper, VisitBooking> implements VisitBookingService {


    @Autowired
    private VisitBookingMapper visitBookingMapper;
    @Autowired
    private InnerDoctorWorkMapper innerDoctorWorkMapper;

    @Autowired
    private InnerDoctorMapper innerDoctorMapper;

    @Autowired
    private VisitMedicalCardMapper visitMedicalCardMapper;



    @Autowired
    private VisitBookingHistoryMapper visitBookingHistoryMapper;

    @Override
    public VisitBookingQueryVO getBooking(String userId, String visitBookingId) {
        VisitBookingQueryVO visitBookingQueryVO = new VisitBookingQueryVO();
        // 获取预约信息
        VisitBooking visitBooking = visitBookingMapper.selectById(visitBookingId);
        if (visitBooking == null) {
            log.info(userId + " 用户查询 "+ visitBookingId+"  不存在");
        }
        Assert.notNull(visitBooking, ResponseEnum.BOOKING_NOT_EXIST_ERROR);
        BeanUtils.copyProperties(visitBooking,visitBookingQueryVO,VisitBookingQueryVO.class);
        // 获取医生信息
        String innerDoctorWorkId = visitBooking.getInnerDoctorWorkId();
        if (innerDoctorWorkId == null) {
            log.info(userId + " 用户查询 "+ innerDoctorWorkId+"  不存在");
        }
        Assert.notNull(innerDoctorWorkId,ResponseEnum.DOCTOR_ID_NOT_EXIST_ERROR);

        InnerDoctorWork innerDoctorWork= innerDoctorWorkMapper.selectById(innerDoctorWorkId);
        Long doctorId = innerDoctorWork.getDoctorId();
        if (doctorId == null) {
            log.info(userId + " 用户查询 "+ doctorId+"  不存在");
        }
        Assert.notNull(innerDoctorWorkId,ResponseEnum.DOCTOR_ID_NOT_EXIST_ERROR);
        InnerDoctor innerDoctor = innerDoctorMapper.selectById(doctorId);
        if (innerDoctor == null) {
            log.info(userId + " 用户查询  数据库中不存在"+ doctorId);
        }
        Assert.notNull(innerDoctor,ResponseEnum.DOCTOR_NOT_NUL_ERROR);
        BeanUtils.copyProperties(innerDoctor,visitBookingQueryVO,VisitBookingQueryVO.class);
        return visitBookingQueryVO;
    }

    @Override
    public ArrayList<VisitBookingQueryVO> getBookingList(String userId) {
        // 根据userId获取cardId
        QueryWrapper<VisitMedicalCard> visitMedicalCardQueryWrapper = new QueryWrapper<>();
        visitMedicalCardQueryWrapper.eq("user_id",userId);
        VisitMedicalCard visitMedicalCard = visitMedicalCardMapper.selectOne(visitMedicalCardQueryWrapper);
        if(visitMedicalCard == null){
            log.info("用户未开卡");
            return  null;
        }
        // 根据cardID获取所有预约记录
        Collection<VisitBooking> visitBookingList = visitBookingMapper.getVisitBookingListByIdAndOrder(visitMedicalCard.getMedicalCardId(), "update_time", "desc");
        visitBookingList.stream().forEach(System.err :: println);
        ArrayList<VisitBookingQueryVO> visitBookingQueryVOS = new ArrayList<>();
        for (VisitBooking visitBooking : visitBookingList) {
            VisitBookingQueryVO visitBookingQueryVO = new VisitBookingQueryVO();

            BeanUtils.copyProperties(visitBooking,visitBookingQueryVO,VisitBookingQueryVO.class);
            String innerDoctorId = visitBooking.getInnerDoctorId();
            InnerDoctor innerDoctor = innerDoctorMapper.selectById(innerDoctorId);
            visitBookingQueryVO.setDoctorName(innerDoctor.getDoctorName());
            visitBookingQueryVO.setDoctorOutpatient(innerDoctor.getDoctorOutpatient());
            visitBookingQueryVOS.add(visitBookingQueryVO);
        }
        visitBookingQueryVOS.stream().forEach(System.err :: println);
        return visitBookingQueryVOS;

    }

    @Override
    public boolean cancelBooking(String userId, String bookingId) {
        // 106 已预约  109 已取消 TODO添加事务
        VisitBooking visitBooking = visitBookingMapper.selectById(bookingId);
        if(visitBooking==null){
            log.info(userId + " 用户取消 " + bookingId + " 不存在");
            return false;

        }
        boolean isUpdate = visitBookingMapper.updateBookingById(bookingId, 109);
        if(isUpdate){
            VisitBookingHistory visitBookingHistory = new VisitBookingHistory();
            visitBookingHistory.setVisitBookingId(bookingId);
            visitBookingHistory.setMedicalCardId(visitBooking.getVisitMedicalCardId());
            visitBookingHistory.setDoctorId(visitBooking.getInnerDoctorId());
            visitBookingHistory.setInnerDoctorWorkId(visitBooking.getInnerDoctorWorkId());
            visitBookingHistory.setBookingStatus(visitBooking.getBookingStatus());
            visitBookingHistory.setRemark("用户取消预约");
            int isSave = visitBookingHistoryMapper.insert(visitBookingHistory);
            return isSave > 0 ;
        }
        return false;
    }



}


